﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RAMED
{
    public partial class RechercherExportésFormcs : Form
    {
        private List<Exportés> ExportésRecherches = new List<Exportés>();
        public RechercherExportésFormcs()
        {
            InitializeComponent();
            RAMEDEntities1 context = SingletonContext.GetContext();
            comboBoxdis.DataSource = context.Distinataires;
            comboBoxdis.DisplayMember = "Nom_Distinataire";
            comboBoxdis.SelectedItem = null;

        }

        private void RechercherBtn_Click(object sender, EventArgs e)
        {

            RAMEDEntities1 context = SingletonContext.GetContext();
            List<Exportés> lesExportés = new List<Exportés>();
            lesExportés = context.Exportés.ToList<Exportés>();


            if (!txtnum.Text.Trim().Equals(""))
            {

                Exportés imp = new Exportés();
                int numeromessage = utitlitaire.CasterString(txtnum.Text);
                lesExportés = lesExportés.Where(p => p.Num_message == numeromessage).ToList<Exportés>();

            }
            if (comboBoxdis.SelectedItem != null)
            {
                Distinataire Dis = new Distinataire();
                Dis = (Distinataire)comboBoxdis.SelectedValue;
                lesExportés = lesExportés.Where(p => p.id_Distinataire == Dis.id_Distinataire).ToList<Exportés>();
            }

            if (checkBoxdatenvo.Checked)
            {
                lesExportés = lesExportés.Where(p => p.Date_envoi.Value.Date == dateTimePickerdateenv.Value.Date).ToList<Exportés>();
            }
            ExportésRecherches = lesExportés;
            mettreAjourFormGridView();
        }
        public void mettreAjourFormGridView()
        {
            dataGridViewexpo.DataSource = null;
            remplirDataGridView();
            dataGridViewexpo.Update();
            dataGridViewexpo.Refresh();
        }
        private void remplirDataGridView()
        {
            dataGridViewexpo.AutoGenerateColumns = false;
            dataGridViewexpo.ColumnCount = 4;
            dataGridViewexpo.Columns[0].HeaderText = "Numero Message";
            dataGridViewexpo.Columns[0].DataPropertyName = "Num_message";

          
            dataGridViewexpo.Columns[1].HeaderText = "Date d'envoi";
            dataGridViewexpo.Columns[1].DataPropertyName = "Date_envoi";

            dataGridViewexpo.Columns[2].HeaderText = "Objet";
            dataGridViewexpo.Columns[2].DataPropertyName = "Objet";

            dataGridViewexpo.Columns[3].HeaderText = "Destinataire";
            dataGridViewexpo.Columns[3].DataPropertyName = "Distinataire";



            dataGridViewexpo.DataSource = ExportésRecherches;
        }

        private void btnmodifierdossier_Click(object sender, EventArgs e)
        {
            if (dataGridViewexpo.CurrentRow == null)
            {
                NotificationTest.NotificationManager nm = new NotificationTest.NotificationManager();
                nm.Show(this, "Veuillez sélectionner un enregistrement pour le modifier", Color.Red, 3000);
            }
            else
            {
                RAMEDEntities1 context = SingletonContext.GetContext();

                Exportés Exp = new Exportés();
                int id_Exporte = ((Exportés)dataGridViewexpo.CurrentRow.DataBoundItem).Id_message_exporté;
                Exp = context.Exportés.Where(d => d.Id_message_exporté == id_Exporte).First();
                ModifierExporteForm MEf = new ModifierExporteForm();

                MEf.EXP = Exp;
                MEf.Rechercheexpform = this;
                MEf.InitialiserChamps();
                MEf.Show();
            }
        }

        private void RechercherExportésFormcs_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
